package com.d404.server;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;
import com.d404.server.SendMessage.Upload;

import im.beans.Message;
import im.notify.server.Notify;
import manager.MessageManager;
import manager.PersistentManager;

/**
 * Servlet implementation class SendPhoto
 */
@MultipartConfig
public class SendPhoto extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SendPhoto() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// response.getWriter().append("Served at:
		// ").append(request.getContextPath());
		String path = "E:/myimages";

		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data, SendMessage.Upload.class);

		Part part = request.getPart("file");
		System.out.println("part:" + path);
		int len = (int) part.getSize();
		System.out.println("len:" + len);

		String file = path + "/" + upload.getFileName();
		InputStream is = part.getInputStream();
		FileOutputStream fos = new FileOutputStream(file);
		System.out.println("准备接收");
		byte[] buf = new byte[len];
		is.read(buf);
		fos.write(buf);
		is.close();
		fos.close();

		// 保存至数据库
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.getCurrentSession();

		Transaction tx = session.beginTransaction();
		try {
			Message msg = getMessage(upload);
			session.save(msg);
			// 发送简单消息
			MessageManager.sendSimpleMessage(upload.getToName(), Notify.PLAIN_MESSAGE);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			// session.close();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	// 获取存储消息
	Message getMessage(Upload upload) {
		Message msg = new Message();
		msg.setFileName(upload.getFileName());
		msg.setFileURI(upload.getFileURI());
		msg.setFrom_id(upload.getFrom_id());
		msg.setFromName(upload.getFromName());
		msg.setIsRead(false);
		msg.setText(upload.getText());
		msg.setTimestamp(upload.getTimestamp());
		msg.setTo_id(upload.getTo_id());
		msg.setToName(upload.getToName());
		msg.setType(upload.getType());
		msg.setTimestamp(System.currentTimeMillis() + "");
		return msg;
	}

}
